package com.sky.controller.user;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserLoginService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("user/user")
@RestController
public class UserLoginController {
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private UserLoginService userLoginService;

    @PostMapping("login")
    public Result userLogin(@RequestBody UserLoginDTO userLoginDTO) {
        UserLoginVO userLoginVO = userLoginService.userLogin(userLoginDTO);

        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, userLoginVO.getId());
        claims.put("openid", userLoginVO.getOpenid());
        String jwt = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        userLoginVO.setToken(jwt);
        return Result.success(userLoginVO);
    }
}
